package com.ky.manage.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.ky.manage.BaseApplication;
import com.ky.manage.R;
import com.ky.manage.base.BaseActivity;
import com.ky.manage.constant.OverallData;
import com.ky.manage.utils.ToastUtils;
import com.ky.manage.utils.Utils;

/**
 * 登录页
 */
public class LoginActivity extends BaseActivity {
    private String mInputPhoneValue = "";
    private TextView mGetSmsCodeTv;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        findView(R.id.back_iv, v -> {
            //返回
            finish();
        });
        EditText phoneEdit = findView(R.id.phone_et);
        phoneEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s , int start , int count , int after) {

            }

            @Override
            public void onTextChanged(CharSequence s , int start , int before , int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mInputPhoneValue = s.toString().trim();
                mGetSmsCodeTv.setEnabled(mInputPhoneValue.length() == 11);
            }
        });
        mGetSmsCodeTv = findView(R.id.get_sms_code_tv, v -> {
            //获取验证码
            if (Utils.CheckMobilePhoneNum(mInputPhoneValue)) {
                ToastUtils.showRoundRectToast("等待验证码返回");
                OverallData.sLoginPhone = mInputPhoneValue;
                toActivity(new Intent(LoginActivity.this, SmsCodeReceiveActivity.class));
            } else {
                ToastUtils.showRoundRectToast("请输入有效手机号");
            }
        });
        findView(R.id.pwd_login_tv, v -> {
            //密码登陆
            toActivity(new Intent(LoginActivity.this, PwdLoginActivity.class));
        });
        ToastUtils.showRoundRectToast("未登录，请登录");
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (BaseApplication.isValidLogin()) {
            finish();
        }
    }
}
